﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core;
using System.Data.Entity.SqlServer;
using System.Linq;
using System.Net.Http;
using System.Web;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json;

namespace WebServiceTMS.Helpers
{
    public class TMSTeams
    {
        public string GoalName { get; set; }
        public TMSUser[] TeamMembers { get; set; }
    }
    public class TeamHelper
    {
        public static HttpResponseMessage GetAllUserTeamsHelper(int userId)
        {
            try
            {
                using (var context = new TMSEntities())
                {
                    var user = context.Users.Find(userId);
                    if (user != null)
                    {
                        var allGoals = (from g in context.Goals
                                        join team in context.Teams on g.Id equals team.Goal
                                        where team.TeamMember == userId
                                        select g).ToArray();
                        var teams = GetTeams(context, allGoals);
                        return HttpResponseHelper.CreateHttpResponse("success", JsonConvert.SerializeObject(teams));
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "There is no such user!");
                }
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        public static HttpResponseMessage GetAllTeamsHelper(int userId)
        {
            try
            {
                using (var context = new TMSEntities())
                {
                    var user = context.Users.Find(userId);
                    if (user != null)
                    {
                        if (user.Status == "Admin")
                        {
                            var allGoals = context.Goals.ToArray();
                            var teams = GetTeams(context, allGoals);
                            return HttpResponseHelper.CreateHttpResponse("success", JsonConvert.SerializeObject(teams));
                        }
                        return HttpResponseHelper.CreateHttpResponse("error", "You have no rights to view this information!");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "There is no such user!");
                }
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        private static TMSTeams[] GetTeams(TMSEntities context, Goals[] allGoals)
        {
            var teams = new TMSTeams[allGoals.Count()];
            for (var i = 0; i < allGoals.Count(); i++)
            {
                var goal = allGoals[i];
                teams[i] = new TMSTeams
                {
                    GoalName = goal.Name
                };
                var query = from team in context.Teams
                    join u in context.Users on team.TeamMember equals u.Id
                    where team.Goal == goal.Id
                    select new TMSUser
                    {
                        Id = SqlFunctions.StringConvert((double) u.Id).Trim(),
                        Name = u.Name,
                        Surname = u.Surname,
                        Phone = u.Phone,
                        Email = u.Email,
                        Login = u.Login,
                        Status = u.Status
                    };
                teams[i].TeamMembers = query.ToArray();
                if (teams[i].TeamMembers.Any())
                {
                    for (var j = 0; j < teams[i].TeamMembers.Count(); j++)
                    {
                        teams[i].TeamMembers[j].Id = TMSCryptography.Encrypt(teams[i].TeamMembers[j].Id);
                    }
                }
            }
            return teams;
        }
    }
}